package com.szty.libarytest.a3papertest

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.ScreenUtils
import com.sjh.lib_app.router.route
import com.sjh.lib_app.utils.color
import com.szty.lib_pen.Utils
import com.szty.lib_pen.oidbluetooth.BluetoothLEService
import com.szty.lib_pen.oidbluetooth.OnDataReceiveListener
import com.szty.libarytest.R
import com.szty.libarytest.R.mipmap.bg_test_a3_1
import com.szty.libarytest.other.loge
import com.tqltech.tqlpencomm.bean.Dot
import kotlinx.android.synthetic.main.activity_a3_paper_test.*

class A3PaperTestActivity : AppCompatActivity() {
    private var mBleService: BluetoothLEService? = null //蓝牙服务

    private val onDataReceiverListener by lazy {
        object : OnDataReceiveListener() {
            override fun onDataReceive(dot: Dot?) {
                if (dot == null) return
                runOnUiThread {
                    Utils.processEachDot(dot,dv_show_test)
                }
            }

            override fun onConnectChange(status: Int) {
                super.onConnectChange(status)
                changeStatus(status == 1)
            }
        }
    }
    private val mServiceConnection by lazy {
        object : ServiceConnection {
            override fun onServiceDisconnected(name: ComponentName?) {}
            override fun onServiceConnected(name: ComponentName, service: IBinder) {
                if (service is BluetoothLEService.LocalBinder) {
                    mBleService = service.service
                }
                mBleService?.apply {
                    addOnDataReceiveListener(onDataReceiverListener)
                }
            }
        }
    }

    private fun changeStatus(isChangePenStatus: Boolean) {
        runOnUiThread {
            tv_un_connect_pen.text = "连接状态:${if (isChangePenStatus) "笔已连接" else "未连接笔"}"
            tv_un_connect_pen.setTextColor(color(if (isChangePenStatus) R.color.app_green else R.color.app_red))
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_a3_paper_test)
        startService(Intent(this, BluetoothLEService::class.java))
        bindService(
            Intent(this, BluetoothLEService::class.java),
            mServiceConnection,
            Context.BIND_AUTO_CREATE
        )
        tv_un_connect_pen.setOnClickListener {
            route("/lib_pen/devConnect")
        }
        tv_get_wh.setOnClickListener {
            dv_show_test.bG_WIDTH.loge()
            dv_show_test.bG_HEIGHT.loge()
        }
        tv_get_pic_size.setOnClickListener {
            "屏幕密度:${ScreenUtils.getScreenDensity()}".loge()
            "屏幕密度dpi:${ScreenUtils.getScreenDensityDpi()}".loge()
            val options = BitmapFactory.Options()
            options.inJustDecodeBounds = true
            BitmapFactory.decodeResource(resources,R.drawable.sp_bg_p0,options)
            options.inSampleSize = 1
            options.inJustDecodeBounds = false
            options.outWidth.loge()
            options.outHeight.loge()
            
        }
    }
}